package com.qifan.alb2b.action.front;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qifan.alb2b.action.back.param.UserQueryParam;
import com.qifan.alb2b.domain.CompanyInfoDomain;
import com.qifan.alb2b.domain.UserInfoDomain;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.CompanyInfoService;
import com.qifan.alb2b.service.UserInfoService;
import com.qifan.alb2b.service.vo.UserVo;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;
import com.qifan.alb2b.util.Stringer;

@Controller
@RequestMapping("/userCenter")
public class UserCenterController{
	private UserInfoService userInfoService;
	private CompanyInfoService companyInfoService;

	public void setCompanyInfoService(CompanyInfoService companyInfoService) {
		this.companyInfoService = companyInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	@RequestMapping("/query.usr")
	@ResponseBody
	public Page query(DataGridModel dgm,UserQueryParam uqp) {
		//spring太给力了，可以自动装配两个对象  会自动的装返回的Map转换成JSON对象
		return userInfoService.query(dgm,uqp);
	}
	
	
	
	@RequestMapping("/update.usr")
	@ResponseBody
	public Map<String, Object> update(HttpSession session, HttpServletRequest request,UserInfoDomain userInfoDomain,CompanyInfoDomain companyInfoDomain){
		UserVo userVo=(UserVo) session.getAttribute(Constants.SESSION_USER_VO);
		UserInfoDomain userInfo=userVo.getUserInfo();
		CompanyInfoDomain companyInfo = userVo.getCompanyInfo();
		
		userInfoDomain.setCompName(companyInfoDomain.getCompName());
		userInfoDomain.setCompId(userInfo.getCompId());//设置唯一ID
		userInfoDomain.setCreateTime(userInfo.getCreateTime());//设置创建日期
		userInfoDomain.setUserId(userInfo.getUserId());
		userInfoDomain.setUserName(userInfo.getUserName());
		userInfoDomain.setUserPwd(userInfo.getUserPwd());
		userInfoDomain.setCompName(companyInfo.getCompName());
		userInfoDomain.setCompId(companyInfo.getCompId());
		userInfoDomain.setStatus(userInfo.getStatus());
		
		
		companyInfoDomain.setCompId(companyInfo.getCompId());
		companyInfoDomain.setCompName(companyInfo.getCompName());
		companyInfoDomain.setCreateTime(companyInfo.getCreateTime());
		try{
			userInfoService.update(userInfoDomain);
			companyInfoService.update(companyInfoDomain);
		}catch(Exception e){
			userInfoService.update(userInfo);
			companyInfoService.update(companyInfo);
		}
		
		
		userVo.setUserInfo(userInfoDomain);
		userVo.setCompanyInfo(companyInfoDomain);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("success", true);	
		return map;
	}
	
	/**
	 * 根据ID获取用户
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryUser.usr")
	public String queryHotel(HttpSession session, HttpServletRequest request, String id){
		
		UserVo userVo=(UserVo) session.getAttribute(Constants.SESSION_USER_VO);
		request.setAttribute("comp",userVo.getCompanyInfo());
		request.setAttribute("user",userVo.getUserInfo());
		return "pages/front/user/update_user";
	}
	
	
	
	/**
	 * 根据用户ID获取用户订单
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryOrder.usr")
	public String queryOrder(HttpServletRequest request, String id){
		
		UserInfoDomain userinfo = (UserInfoDomain)request.getSession().getAttribute("USER");
		request.setAttribute("order",userInfoService.getOrderByUserId(userinfo.getUserId()));
		request.setAttribute("user",userinfo);
		return "pages/front/user/orderList";
	}
	
	
	
	@RequestMapping("/updatePassword.usr")
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request,String password,String password1){
		UserVo uservo = (UserVo)request.getSession().getAttribute(Constants.SESSION_USER_VO);
		password=Stringer.md5(password).substring(0,30);
		password1=Stringer.md5(password1).substring(0,30);
		
		UserInfoDomain userinfo=uservo.getUserInfo();
		if(!password.equals(userinfo.getUserPwd())){
			throw new BusinessException("原密码输入错误！");
		}
		
		Map<String, Object> map=new HashMap<String, Object>();
		userinfo.setUserPwd(password1);
		userInfoService.update(userinfo);
		request.getSession().setAttribute("USER",userinfo);
		map.put("success", true);	
		return map;
	}
	
}
